package org.training.apr.dao.fake;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;


/**
 * @author Miguel Guinada
 * 
 * This fake DAO implementation is deprecated becuase of it's 
 * intrusive nature making the model implement the Identifiable interface 
 */
@Deprecated
public abstract class IntrusiveFakeDao<T extends Identifiable> {
	private Map<Long, T> map = new TreeMap<Long, T>();
	
	public IntrusiveFakeDao() {
	}
	
	protected List<T> uniqueResult(List<T> list) {
		if(list.size() > 1) {
			throw new FakeDaoException("collection should not have more than one result");
		}
		return list;
	}
	
	public void save(T t) {
		map.put(t.getId(), t);
	}
	
	public T read(Long id) {
		return map.get(id);
	}
	
	public void delete(T t) {
		map.remove(t.getId());
	}
	
	public List<T> findAll() {
		return new ArrayList<T>(map.values());
	}

	public List<T> findByCriteria(Criteria<T> criteria) {
		List<T> result = new ArrayList<T>();
		
		for(T t: findAll()) {
			if(criteria.evaluate(t)) {
				result.add(t);
			}
		}
		
		return result;
	}
	
	public void dump() {
		for(T t : findAll()) {
			System.out.println(t);
		}
	}
}